{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "authorship_tag": "ABX9TyML7UXVR5Vtg/YyvdwPkS+v",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/sugarforever/LangChain-Tutorials/blob/main/LangChain_Pinecone_Serverless.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "py_LzY9P5qrs",
        "outputId": "56faba32-8eff-4487-badd-8c3e4271681d"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.6/1.6 MB\u001b[0m \u001b[31m12.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m199.9/199.9 kB\u001b[0m \u001b[31m10.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m222.3/222.3 kB\u001b[0m \u001b[31m10.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m218.6/218.6 kB\u001b[0m \u001b[31m11.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m48.4/48.4 kB\u001b[0m \u001b[31m2.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m75.9/75.9 kB\u001b[0m \u001b[31m4.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m49.4/49.4 kB\u001b[0m \u001b[31m3.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m76.9/76.9 kB\u001b[0m \u001b[31m5.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m3.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25h\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n",
            "llmx 0.0.15a0 requires cohere, which is not installed.\n",
            "llmx 0.0.15a0 requires tiktoken, which is not installed.\n",
            "tensorflow-probability 0.22.0 requires typing-extensions<4.6.0, but you have typing-extensions 4.9.0 which is incompatible.\u001b[0m\u001b[31m\n",
            "\u001b[0m"
          ]
        }
      ],
      "source": [
        "!pip install langchain-community==0.0.13 pinecone-client==3.0.0 openai -q -U"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "from google.colab import userdata\n",
        "\n",
        "PINECONE_INDEX_NAME = 'serverless-example'\n",
        "\n",
        "import os\n",
        "\n",
        "os.environ['OPENAI_API_KEY'] = userdata.get('OPENAI_API_KEY')\n",
        "os.environ['PINECONE_API_KEY'] = userdata.get('PINECONE_API_KEY')"
      ],
      "metadata": {
        "id": "0pmV2F7i686r"
      },
      "execution_count": 9,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "from langchain_community.chat_models import ChatOpenAI\n",
        "from langchain_community.embeddings import OpenAIEmbeddings\n",
        "from langchain_community.vectorstores import Pinecone\n",
        "from langchain_core.output_parsers import StrOutputParser\n",
        "from langchain_core.prompts import ChatPromptTemplate\n",
        "from langchain_core.runnables import RunnableParallel, RunnablePassthrough, RunnableLambda\n",
        "from pinecone import Pinecone as PineconeClient"
      ],
      "metadata": {
        "id": "MKYDbcUm7YCe"
      },
      "execution_count": 7,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "vectorstore = Pinecone.from_existing_index(index_name=PINECONE_INDEX_NAME, embedding=OpenAIEmbeddings())"
      ],
      "metadata": {
        "id": "CCEL7hig7mha"
      },
      "execution_count": 10,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "!wget -O Optimism_and_StarkNet.pdf https://arxiv.org/pdf/2210.16610.pdf"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "65ctMUEG9gcJ",
        "outputId": "9f8d791d-7919-4681-f093-74a9f9266969"
      },
      "execution_count": 12,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "--2024-01-17 19:49:34--  https://arxiv.org/pdf/2210.16610.pdf\n",
            "Resolving arxiv.org (arxiv.org)... 151.101.67.42, 151.101.3.42, 151.101.195.42, ...\n",
            "Connecting to arxiv.org (arxiv.org)|151.101.67.42|:443... connected.\n",
            "HTTP request sent, awaiting response... 200 OK\n",
            "Length: 505763 (494K) [application/pdf]\n",
            "Saving to: ‘Optimism_and_StarkNet.pdf’\n",
            "\n",
            "Optimism_and_StarkN 100%[===================>] 493.91K  --.-KB/s    in 0.04s   \n",
            "\n",
            "2024-01-17 19:49:35 (11.1 MB/s) - ‘Optimism_and_StarkNet.pdf’ saved [505763/505763]\n",
            "\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "!pip install -q -U langchain pypdf tiktoken"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "pUYwwzxZ-RkL",
        "outputId": "f685f33f-8dbb-48d8-e798-387d0d4bb6c9"
      },
      "execution_count": 19,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\u001b[?25l     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/2.0 MB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K     \u001b[91m━\u001b[0m\u001b[91m╸\u001b[0m\u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.1/2.0 MB\u001b[0m \u001b[31m2.4 MB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m\r\u001b[2K     \u001b[91m━━━━━━━━━\u001b[0m\u001b[91m╸\u001b[0m\u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.5/2.0 MB\u001b[0m \u001b[31m7.3 MB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m\r\u001b[2K     \u001b[91m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[90m╺\u001b[0m\u001b[90m━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.0/2.0 MB\u001b[0m \u001b[31m9.8 MB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m\r\u001b[2K     \u001b[91m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[91m╸\u001b[0m\u001b[90m━━━━━━━━\u001b[0m \u001b[32m1.6/2.0 MB\u001b[0m \u001b[31m11.5 MB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m\r\u001b[2K     \u001b[91m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[91m╸\u001b[0m \u001b[32m2.0/2.0 MB\u001b[0m \u001b[31m12.4 MB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m\r\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.0/2.0 MB\u001b[0m \u001b[31m11.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25h\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n",
            "llmx 0.0.15a0 requires cohere, which is not installed.\u001b[0m\u001b[31m\n",
            "\u001b[0m"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "from langchain_community.document_loaders import PyPDFLoader\n",
        "\n",
        "loader = PyPDFLoader(\"Optimism_and_StarkNet.pdf\")\n",
        "documents = loader.load()"
      ],
      "metadata": {
        "id": "6rZwAeQR-WBQ"
      },
      "execution_count": 15,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "from langchain.text_splitter import RecursiveCharacterTextSplitter\n",
        "text_splitter = RecursiveCharacterTextSplitter(\n",
        "    chunk_size=1000,\n",
        "    chunk_overlap=200,\n",
        "    length_function=len,\n",
        "    is_separator_regex=False,\n",
        ")\n",
        "split_docs = text_splitter.split_documents(documents)"
      ],
      "metadata": {
        "id": "Eja9BBYR-vd0"
      },
      "execution_count": 17,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "vectorstore.add_documents(split_docs)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "HaxTcv3o_XyM",
        "outputId": "29b49e66-24ec-4f1d-d8d8-b9d626b35a36"
      },
      "execution_count": 20,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "['f5e75a8e-324c-43ea-b4da-5ab514fd96d0',\n",
              " '22747fb7-d14c-46c6-ba93-8ce35f21dddf',\n",
              " 'c1631bf3-4950-4c80-942b-406172b68f62',\n",
              " 'd15d89e6-bf52-4d23-b50b-2a5f96975693',\n",
              " 'eb1b7a14-51a0-4f9d-a3ff-dda95baae02c',\n",
              " '521b82dc-0ba9-47d8-9e5f-175313640348',\n",
              " '9309945e-a415-4e74-ae00-034b63d6d639',\n",
              " '41cba134-ed92-476d-a1a8-64584f15edef',\n",
              " '558fdcb4-a891-4896-8b13-7c21089f1a02',\n",
              " '8ec311af-dd62-4dd5-af9d-3bc064923285',\n",
              " 'be4842d4-7405-4b3f-8a5c-81cf8bb434f2',\n",
              " '402317c3-b14d-45db-9c0b-591970d59ebd',\n",
              " '5bb0366a-9df9-49fc-be6a-08df51c3fd5f',\n",
              " 'cee2c5bc-406d-4020-addc-76cba22657cc',\n",
              " 'ff5be6ea-916d-4149-a5bf-70723ec9c3f2',\n",
              " '3e4b2181-508e-4b12-80cf-4470bdddc1fa',\n",
              " 'c970cc8a-65b3-4b54-8ac8-2dae2d5a73d2',\n",
              " '7c4d6831-e20a-408d-a87c-734bb7130dd0',\n",
              " '21aaf5dd-0335-4516-807c-5f747ac4a22b',\n",
              " 'a1e7d351-c391-4602-b0ce-94b7050079be',\n",
              " '1e6a2dd4-3ced-4a47-a633-19fc4b8294d9',\n",
              " '53d9f9f9-d768-4a6f-a279-f56b52a4896d',\n",
              " '5bff99ee-241d-4fca-87b0-c17360f156de',\n",
              " '23e7e451-bb06-4c4a-b445-8974bd135455',\n",
              " '23de79e9-8763-4c22-bd80-ce66976c2bce',\n",
              " '0e059d4f-6313-4dc9-a035-4da6d01cac66',\n",
              " 'd14badab-0a27-48cb-ab3a-97e164bdd0e4',\n",
              " 'b5b3c0aa-23d1-441f-ad6c-7369d9b99213',\n",
              " 'e7b37470-ce1d-450f-a86c-7936852d9487',\n",
              " '9d73efa7-920b-4db6-bc68-decad7d0806d',\n",
              " '2f9302ef-ad38-4478-ae8c-32312a211160',\n",
              " '35334d36-bbe1-4bfd-9de7-e6e6682012e2',\n",
              " 'a65b311f-d952-48a6-a8cf-e8cd5f81bc6b',\n",
              " 'e9e1bf12-d115-4a60-8c0a-4d925cbf6fac',\n",
              " '93e0457e-9c0f-475f-9cdc-4dcfdfdcdbf4',\n",
              " '80e4866f-9990-4003-8644-f953ce36e173',\n",
              " '1315796d-45e8-4cbd-8fc9-d1c4e2e1e11e',\n",
              " '007adb44-afbc-4900-9483-2b2f9d851cb6',\n",
              " '098808ac-00a0-4302-b8a7-36c4ad662867',\n",
              " '00c9fb2b-d9f1-47bb-84dc-0c1871983fce',\n",
              " '89caf5de-e1a4-4366-9e44-782d16402795',\n",
              " '3e64b4fc-7a42-4390-a78a-19a79b6b110c',\n",
              " 'e4e407e0-b265-4876-8328-9406144bac63',\n",
              " 'fa5f6dcd-e68e-4ec7-947b-f3b1f5432962',\n",
              " 'ea9920c2-0b9a-4e63-ba21-4c3c498c7d5f',\n",
              " 'b7691a82-8821-4c4e-981d-9b6c3b2e8f57',\n",
              " '12a335f0-d6aa-4b2e-8453-a0006085a4f5',\n",
              " '6c2ca28c-032d-4747-88f6-fc91fe0c5bbc',\n",
              " 'c66dd608-5878-46dd-abff-bbf4e28a9668',\n",
              " 'be8f49a4-f302-436d-a9de-2c7ab393b4e3',\n",
              " '9f12e18e-703f-424f-8bcd-9a08aeec1a1c',\n",
              " '0b0e3ee6-cda9-4335-b222-99a669c7b73d',\n",
              " '67d03fc9-e4b4-4f5c-af2f-a5bec33329c5',\n",
              " '2e6a6fc9-aa53-43e2-bb78-66e89de21f64',\n",
              " '5998a12d-fe87-4fac-a1df-b06e34b289fb',\n",
              " 'bbb783b9-b555-44be-91a2-0d341ad8431c',\n",
              " '7da33aa6-1efc-4030-8709-a30967069965',\n",
              " '47708085-431c-44c7-9fa3-3507549c8f83',\n",
              " '0dd9da3f-8563-4d0c-929e-737fd7f0603e',\n",
              " '9b515b02-0f37-4d88-b38b-3e56eb03855c',\n",
              " '5461c7e5-8705-4ca5-8e79-f90f616c5e7e',\n",
              " 'dd17d46f-40e9-4902-a3e7-77fb86ad1244',\n",
              " 'a4a2e987-4abe-400e-a48e-ec205f914d01',\n",
              " '8bef1a65-834d-48d7-a31a-915851996200',\n",
              " '7b9ec1e5-cdc2-443f-8c1f-57ad64946acd',\n",
              " 'b1ea798b-4254-4a1b-a80c-20355e563e2f',\n",
              " 'b0657202-6686-4770-a73a-ada686128f37',\n",
              " '866231bb-1113-4cd4-80fc-078f41ac9367',\n",
              " 'ba5c8418-8656-488f-9e1b-62cba9d4c101',\n",
              " 'd65ca6d4-5c19-44aa-9f1f-42ec28968307',\n",
              " 'e979362f-d499-485f-8da7-4d048c07a260',\n",
              " '38398818-71db-4293-b7dd-3cc695b9561c',\n",
              " '7ff5e07d-aef2-4110-8f6d-b6371f1f75ba',\n",
              " 'da175a12-0b50-4127-90ce-5936754b466d',\n",
              " '04e5e6dd-ff12-4037-8b76-dbe954bee788',\n",
              " 'c9cda3d1-4186-4543-b1bc-1ef01c5e5558',\n",
              " '5b3de5c2-a8ec-4496-afc2-44b5f38bb5b4',\n",
              " 'beead59e-aeae-4aa2-9188-b006b947afe6',\n",
              " 'd922e06a-7b5e-4b16-b265-1a2611fec1ec',\n",
              " 'bbc16afe-18d2-4b1a-b276-9daac949c10e',\n",
              " '667c8b05-72f0-42a9-be7b-aca5f563a141',\n",
              " 'a00d7795-8831-49e4-b142-c3fb207c52c6',\n",
              " '01b64cd0-849b-445f-88a2-fd85bc4bfd53',\n",
              " 'f07a3bb8-a097-43f0-b5af-82b5b42b6bcb',\n",
              " '1e3ebd21-9d64-4fce-a5da-a120a94fe5c2',\n",
              " 'f21374b7-543a-4494-9062-7e0b24aa53a3',\n",
              " '5612e9b7-5d18-44e5-9650-9f87a2a2d5df',\n",
              " '71e678a1-a70c-4f45-8d7e-a99d500644b4',\n",
              " 'e7754104-b701-45e9-9a5b-af1b959a442d',\n",
              " '91e27b09-14f9-4fb1-8005-593890424d83',\n",
              " '13dc2cac-975f-44a6-8f16-127a27e204b9',\n",
              " 'bbf5ff84-c251-4468-a1ca-80caa0add004',\n",
              " '7c5f187d-934a-49be-89e0-7d8653a45f29',\n",
              " '347f7d40-badd-4f89-9843-f8b9ddcf4a91',\n",
              " 'c9f9f7d4-2bd3-4bf4-a743-e755c11e405c',\n",
              " '7af4c35d-e749-496d-b21e-30f057d01b55',\n",
              " '80bf999f-415c-46ed-8385-f73ea7f320a4',\n",
              " '45d1ee93-1626-4ee9-be10-03b05258d469',\n",
              " '5d689a9d-0248-41b0-a025-af93ffec870b',\n",
              " '07faff36-8f70-429f-a5c8-c40ac21a3d36',\n",
              " '8585f083-b158-4252-84fc-9bdd56ddf7d8',\n",
              " '1f20d6f9-d80a-4458-b64a-75f8f5cb1847',\n",
              " '7e29cd1c-375d-4e03-aac3-43d168667787',\n",
              " 'e10c40c3-4c0b-488e-9578-7b47e29bde82',\n",
              " '7b3430b6-dd11-42da-bb4f-f7386d5e3032',\n",
              " 'aa4fa059-7805-4cd0-be76-93b1b4be91b6',\n",
              " '91fae641-afe2-49e9-a1d3-3d5931ff90e9',\n",
              " '06e84a78-e988-48c5-afdf-c2fe9a8917c5',\n",
              " '8b004b60-eb3e-4f33-a33d-faa9a3356924',\n",
              " 'c6893f70-e42a-4739-88c5-9df8bc58ef19',\n",
              " '4f289bc5-62b6-4b10-9285-de38fcc3f098',\n",
              " '79c9d6d3-3aef-4138-a20b-0e76903a027b',\n",
              " 'df6190bb-5da4-45d8-b138-c3a84bc4f689',\n",
              " 'e2cc2544-6147-4f1a-97b9-b8a0aa44426b',\n",
              " '9b0f41da-1b16-486e-be08-686f8975ef64',\n",
              " '6a2476dd-be70-4405-b764-efaefd0c6715',\n",
              " '6d680fda-37cd-4ab4-8cf0-3c22a41fd492',\n",
              " '4143c59d-62cb-4d5b-94bf-6c9ce64a819d',\n",
              " '36704b59-125f-4f3a-9f54-468d86aa72ab',\n",
              " '64986407-726c-43cb-9709-8dc0e0592311',\n",
              " '25bf014d-6e54-4db2-a298-504ed536ba63',\n",
              " 'd656d1eb-f0f2-44eb-a223-9aef8b6a3ec9',\n",
              " 'a5818b51-c655-4405-9ff5-9250126f2002',\n",
              " 'ad6c12be-df53-4d1b-9afb-b6b2d2c17451',\n",
              " '67576102-1b11-4cec-a82b-0565843c899d',\n",
              " '1fd22660-2b1f-4fa5-bc4b-999d46813a04',\n",
              " '11d8c13c-150a-4910-851d-4a84f9b13747',\n",
              " '62b42a67-d92b-438d-b128-8b754087eb4e',\n",
              " '4c930e3a-45ae-468e-8bb1-7c57c89abc4b',\n",
              " 'f2d87cde-6182-48b8-93ec-05ac72a05d09',\n",
              " '7203e561-b884-497a-b116-e605e585e167',\n",
              " '1e1c1bb0-17bf-437d-8826-e1c9a0120835',\n",
              " '6d9fe4e3-d2a8-438c-b179-9625e50a7ece',\n",
              " 'df9d8d58-5072-438d-b776-08f20076981c',\n",
              " 'bf0379d5-eb20-490a-8ea5-aec173c7f686',\n",
              " 'e50e90f5-9916-4793-b48e-2df3f7140535',\n",
              " '0de84b19-94a3-4843-a3af-a512d44e2d7d',\n",
              " 'dc4afe15-3f46-4f8b-b466-a45dffc54846',\n",
              " '2245486b-0834-4815-b393-9fb1ae71f675',\n",
              " '937ab5cb-33ba-4a90-9257-bac968951c86',\n",
              " '270ae3ec-c2f5-4222-a22c-25b7686ff6ba',\n",
              " '2bfc6f75-4f91-414b-8347-f990d96a6e91',\n",
              " 'ad947bd2-2c78-42e8-b02d-e2b806b80426',\n",
              " 'd2195687-a0e9-4338-82b4-f5be16967267',\n",
              " 'd45e0dd4-49ca-4968-82bc-a1fb6b6a0117',\n",
              " '13d95c05-111a-4eec-b5c9-46885c110700',\n",
              " '2a33eab8-9318-47be-9a35-a637ecbb1004',\n",
              " '7cd696fc-a5cf-47e1-910c-cd287ccd25a2',\n",
              " 'fc2aeb15-7a28-4b95-be9b-6f6d5ae9665e',\n",
              " '889443d0-1d8e-4624-8c7f-5eb66ed2f3d8',\n",
              " '933c9b99-181e-4ebb-a0f0-1593b1875fa9',\n",
              " '8826eeef-e0ef-45e9-ac9e-07a59657dbbd']"
            ]
          },
          "metadata": {},
          "execution_count": 20
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "retriever = vectorstore.as_retriever()"
      ],
      "metadata": {
        "id": "bFeI4n45_fxM"
      },
      "execution_count": 21,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "template = \"\"\"Answer the question based only on the following context:\n",
        "{context}\n",
        "Question: {question}\n",
        "\"\"\"\n",
        "\n",
        "chain = (\n",
        "    RunnableParallel({\"context\": retriever, \"question\": RunnablePassthrough()})\n",
        "    | ChatPromptTemplate.from_template(template)\n",
        "    | ChatOpenAI(temperature=0, model=\"gpt-3.5-turbo-16k\")\n",
        "    | StrOutputParser()\n",
        ")"
      ],
      "metadata": {
        "id": "8RgJSst8_3qU"
      },
      "execution_count": 23,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "chain.invoke(\"What is optimisitic rollup?\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 54
        },
        "id": "5FI8gWrfAEVb",
        "outputId": "392c222a-1dd7-44e5-d31b-13c97288795a"
      },
      "execution_count": 27,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'Optimistic Rollup is a scalability solution for decentralized blockchains that locks the most value. It is based on the Ethereum Virtual Machine (EVM) and allows for strong development due to compatibility with existing Ethereum tools and the use of Solidity. It is a type of Rollup that accepts the output of blocks optimistically without verifying their execution.'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 27
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "chain.invoke(\"What is the conclusion?\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 54
        },
        "id": "cZ5eQj6fIqoz",
        "outputId": "0203bedd-235d-4a46-fdbc-e42f60a9cee9"
      },
      "execution_count": 29,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'The conclusion is that rollups are the most promising solution available today to solve the scalability problem in decentralized blockchains, ushering in the era of modular blockchains as opposed to monolithic blockchains.'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 29
        }
      ]
    }
  ]
}